﻿// This Class has been taken from Droid Explorer <http://de.codeplex.com/license>
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Xml.Serialization;

namespace DroidManager.Android
{
    /// <summary>
    /// Used to read Aapt Output
    /// </summary>
    public abstract class CommandResult
    {
        private StringBuilder _output;
        private StringBuilder _special;

        internal CommandResult()
        {
            this.Output = new StringBuilder();
            this.SpecialOutput = new StringBuilder();
        }

        [XmlIgnore]
        internal StringBuilder Output
        {
            get { return _output; }
            private set { _output = value; }
        }

        [XmlIgnore]
        internal StringBuilder SpecialOutput
        {
            get { return this._special; }
            private set { _special = value; }
        }

        [XmlIgnore]
        internal bool HasSpecialOutput
        {
            get
            {
                return
                    string.Compare(this.SpecialOutput.ToString(), string.Empty, true, CultureInfo.InvariantCulture) != 0;
            }
        }

        /// <summary>
        /// Process Aapt Output
        /// </summary>
        /// <param name="data">Aapt Output</param>
        protected virtual void ProcessData(string data)
        {
            Regex regex = new Regex(@"^error\:\s+(.+)$",
                                    RegexOptions.Compiled | RegexOptions.IgnoreCase |
                                    RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
            Match m = regex.Match(data);
            //if (m.Success)
            //{

            //}

            string[] split = data.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
            regex = new Regex(@"^\*\s?(.+)\s?\*",
                              RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase |
                              RegexOptions.IgnorePatternWhitespace);
            foreach (string var in split)
            {
                if (regex.IsMatch(var))
                {
                    this.SpecialOutput.AppendLine(var);
                }
                else
                {
                    this.Output.AppendLine(var);
                }
            }
        }
    }
}